<template>
  <a-modal
    title="修改广告"
    width="40%"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    :destroyOnClose="true"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <h3 style='text-align: center;'>{{record.name}}</h3>
        <a-divider />
        <a-form-item label="广告名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select
            v-decorator="['adId',  {rules: [{required: true, message: '请选择广告'}]}]"
            show-search

            :default-active-first-option="false"
            :show-arrow="false"
            :filter-option="false"
            :not-found-content="null"
            :options="adList"
            @search="getOptions"
          >
          </a-select>
        </a-form-item>
      </a-form>


    </a-spin>
  </a-modal>
</template>

<script>
/** @var {BookActionType} BookActionType */
import BookActionType from '@/common/enum/bookActionType'
import * as Api from '@/api/ad'

export default {
  name: 'changeAd',
  data(){
    return {
      BookActionType,
      type:BookActionType.IMAGE_NODE.value,
      // 标签布局属性
      labelCol: { span: 7 },
      // 输入框布局属性
      wrapperCol: {
        span: 13
      },
      // 正在加载
      isLoading: false,
      bookId:'',
      // modal(对话框)是否可见
      visible: false,
      // modal(对话框)确定按钮 loading
      confirmLoading: false,
      form: this.$form.createForm(this),
      adList:[],
      record:{},
    }
  },
  methods:{
    open (record) {
      // 显示窗口
      this.visible = true
      // 当前记录
      this.record = record || {}
      this.getOptions()
    },
    getOptions(name){
      Api.getOptions({name}).then(res=>{
        this.adList=res.data
      })
    },

    /**
     * 确认按钮
     */
    handleSubmit (e) {
      e.preventDefault()
      // 表单验证
      const { form: { validateFields } } = this
      validateFields((errors, values) => {
        // 提交到后端api
        if (!errors) {
          this.onFormSubmit(values)
        }
      })
    },

    /**
     * 提交到后端api
     */
    onFormSubmit (values) {
      this.confirmLoading = true
      values.id = this.record.id
      Api.changeBind(values)
        .then((result) => {
          // 显示成功
          this.$message.success(result.message, 1.5)
          // 关闭对话框事件
          this.handleCancel()
          // 通知父端组件提交完成了
          this.$emit('handleSubmit', values)
        })
        .finally((result) => {
          this.confirmLoading = false
        })
    },
    /**
     * 关闭对话框事件
     */
    handleCancel () {
      this.visible = false
    },
  }
}
</script>

<style scoped>

</style>